﻿using System;
using System.Windows.Forms;

namespace Buy_Fruit
{
    public partial class Form1 : Form
    {
        
        public Form1()
        {
            InitializeComponent();
        }
        Form2 form2 = new Form2();
        //计算按钮
        private void button1_Click(object sender, EventArgs e)
        {
            Calculate(sender,e);
        }
        //计算结果 
        private void Calculate(object sender, EventArgs e)
        {
            //记录结果
            string Text = "";
            //文本框不得为空
            if (textBox1.Text != "" && textBox2.Text != "" && textBox3.Text != "" && textBox4.Text != "" && textBox5.Text != "")
            {
                if (form2.IsDisposed == true)
                {
                    System.Windows.Forms.Application.Restart();
                }
                else
                {
                    form2.Show();
                }
                //获取文本框的值
                double TotalPrice = double.Parse(textBox1.Text.ToString());
                double Apple = double.Parse(textBox2.Text.ToString());
                double Watermelon = double.Parse(textBox3.Text.ToString());
                double Cherry = double.Parse(textBox4.Text.ToString());
                double Durian = double.Parse(textBox5.Text.ToString());
                //每种水果都需要买一次 余款再次分配
                double Balance = TotalPrice - Apple - Watermelon - Cherry - Durian;
                if (Balance < 0)
                {
                    form2.Box("输入的总价不能买到每种水果！请重新输入！！");
                }
                //计算余款买每种水果能买多少个
                double AppleNumber = Balance / Apple;
                Console.WriteLine("{0}", AppleNumber);
                double WatermelonNumber = Balance / Watermelon;
                Console.WriteLine("{0}", WatermelonNumber);
                double CherryNumber = Balance / Cherry;
                Console.WriteLine("{0}", CherryNumber);
                double DurianNumber = Balance / Durian;
                Console.WriteLine("{0}", DurianNumber);

                int Dollars75, Dollars50, Dollars25, Dollars10;
                int Time = 0;
                for (Dollars75 = 1; Dollars75 <= DurianNumber + 1; Dollars75++)
                {
                    for (Dollars50 = 1; Dollars50 <= CherryNumber + 1; Dollars50++)
                    {
                        for (Dollars25 = 1; Dollars25 <= WatermelonNumber + 1; Dollars25++)
                        {
                            for (Dollars10 = 1; Dollars10 <= AppleNumber + 1; Dollars10++)
                            {
                                if (Dollars75 * Durian + Dollars50 * Cherry + Dollars25 * Watermelon + Dollars10 * Apple == TotalPrice)
                                {
                                    Time++;
                                    Console.WriteLine("{0}\t{1}\t{2}\t{3}\n", Dollars75, Dollars50, Dollars25, Dollars10);
                                    Text = Text + TotalPrice + "元买" + Dollars10 + "个苹果，" + Dollars25 + "个西瓜，" + Dollars50 + "个车厘子，" + Dollars75 + "个榴莲。\r\n";
                                }
                            }
                        }
                    }
                }
                Console.WriteLine("一共有{0}种买法", Time);
                Text = Text + "一共有" + Time + "种买法";
                form2.Box(Text);
            }
            else
            {
                return;
            }
        }
        //限制文本框只能输入数字
        private void LimitNumbers(KeyPressEventArgs e)
        {
            if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8)
            {
                e.Handled = true;
                MessageBox.Show("请输入数字0-9有效数值！！");
            }
        }

        //触发事件
        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            LimitNumbers(e);
        }

        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            LimitNumbers(e);
        }

        private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
        {
            LimitNumbers(e);
        }

        private void textBox4_KeyPress(object sender, KeyPressEventArgs e)
        {
            LimitNumbers(e);
        }

        private void textBox5_KeyPress(object sender, KeyPressEventArgs e)
        {
            LimitNumbers(e);
        }
        
    }
}
